//
//  MineServer.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/28.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "MineServer.h"
#import "TimeHelper.h"
@implementation MineServer
- (void)feedbackComplete:(void(^)(BOOL success,NSString*msg))complete{
    if (!self.feedbackType || !self.content.length || !self.contact.length || !self.contact_way.length) {
        return;
    }
    [NetworkManager POST:URL_mine_feedback parameters:@{@"type":@(self.feedbackType),@"content":self.content,@"contact":self.contact,@"contact_way":self.contact_way} responseCache:nil success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            !complete?:complete(YES,base.message);
        }else{
            !complete?:complete(NO,base.message);
        }
    } failure:^(NSError *error) {
        !complete?:complete(NO,kNetworkError);
    }];
}

- (void)getAboutUsComplete:(void(^)(BOOL success,NSString*msg))complete{
    [NetworkManager GET:URL_mine_about_us parameters:@{} responseCache:nil success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.aboutUsArr = [NSMutableArray arrayWithArray:[NSArray modelArrayWithClass:AboutUsModel.class json:base.data]];
            !complete?:complete(YES,base.message);
        }else{
            !complete?:complete(NO,base.message);
        }
    } failure:^(NSError *error) {
        !complete?:complete(NO,kNetworkError);
    }];
}

- (void)getNodeListComplete:(void(^)(BOOL success,NSString*msg))complete{
    [NetworkManager GET:URL_mine_eos_node parameters:@{} responseCache:nil success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.nodeArr = [NSArray modelArrayWithClass:AboutUsModel.class json:base.data];
            !complete?:complete(YES,base.message);
        }else{
            !complete?:complete(NO,base.message);
        }
    } failure:^(NSError *error) {
        !complete?:complete(NO,kNetworkError);
    }];
}
- (void)getUpgradeVer:(NSString*)ver ompletion:(void(^)(BOOL success,NSString *msg))completion{
    if (!ver.length) {
        return;
    }
    [NetworkManager GET:URL_upgrade parameters:@{@"ver":ver} responseCache:nil success:^(id responseObject) {
          XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.updateM = [UpdateModel modelWithJSON:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)getActionListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    NSInteger size = 20;
    params[@"page"]=@(page);
    params[@"account_name"]=self.account;
    params[@"size"] = @(size);
    if (self.symbol.length) {
        params[@"symbol"] = self.symbol;
    }
    self.latestParams = params;
    [PPNetworkHelper setValue:@"5b4added-e80c-41fb-b5a9-16269d2de79b" forHTTPHeaderField:@"apikey"];
    [PPNetworkHelper POST:@"https://open-api.eos.blockdog.com/v2/third/get_account_transfer" parameters:params  responseCache:nil success:^(id responseObject) {
//        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
//        if ([base.errNo isEqualToNumber:@0]) {
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.transcationList removeAllObjects];
            }
            NSInteger trace_count = [[responseObject objectForKey:@"total"] integerValue];
            NSArray *trace_list = [responseObject objectForKey:@"list"];
            [self.transcationList addObjectsFromArray:[NSArray modelArrayWithClass:[TranscationModel class] json:trace_list]];
            !completion?:completion(YES,self.cmtPage*size<trace_count,@"");
//        }else{
//            !completion?:completion(NO,[base.errNo isEqualToNumber:@429]?YES:NO,base.errmsg);
//        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getAllValuationIsMore:(BOOL)isMore completion:(void(^)(BOOL success,NSString *msg))completion{
    
    [NetworkManager GET:URL_token_all_valuation parameters:@{@"accounts":VALIDATE_STRING(self.accounts)} responseCache:^(id responseCache) {
        
    } success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.valuationModel = [ValuationModel modelWithJSON:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);

    }];
    
}
- (NSMutableArray<TranscationModel*>*)transcationList{
    if (!_transcationList) {
        _transcationList = [NSMutableArray array];
    }
    return _transcationList;
}
@end
